/**
 * Created by Administrator on 2016/5/19.
 */
$(function() {
    vsid = '';
    /*$(window).load(function(){
        var $noleftbordernew=$('#signin_email');
        if($noleftbordernew.val())
            $noleftbordernew.addClass('noleftbordernew');

    });
    $('#signin_email').keydown(function(){
        if($(this).hasClass('noleftbordernew'))
            $(this).removeClass('noleftbordernew');
    })*/
    //网站头部的监听程序
    addLisenerForHeader();
    verifyLogin();
    // 切换验证码/密码登陆
    var loginTypeEle = $('#change_login span'), loginType = 'valid_login';
    loginTypeEle.click(function () {
        if(!$(this).hasClass('active')){
            $('.login-error').html('');

            loginType = $(this).attr('id');

            if(loginType == 'valid_login'){
                $('.valid-show').show();
                $('.pw-show').hide();
            }
            if(loginType == 'password_login'){
                $('.valid-show').hide();
                $('.pw-show').show();
            }

            loginTypeEle.removeClass('active');
            $(this).addClass('active');

        }
    });
    $('#signin_email').blur(function () {
        $('.phone-error').html('');
        var phone = $("input[name='phone']").val().trim();
        if(phone){
            var prefixPhoneNum = $("#prefixPhoneNum").attr('value');
            var phoneresult = {}
            console.log(phone)
            if (prefixPhoneNum=='0086') {
                phoneresult = verification.phone(phone)
            } else {
                phoneresult = verification.isNumber(phone)
            }
            if(phoneresult.status_code!=0){
                $('.phone-error').html($.i18n.prop('51fd889cc8b548676309c4be8130b2d2'));
            }
        }
    });
    $('#variCode').blur(function () {
        $('.code-error').html('');
        var veriCode = $("input[name='veriCode']").val().trim();
        if(veriCode){
            var reg = /^\d{4}$/;
            if(!reg.test(veriCode)){
                $('.code-error').html($.i18n.prop('09143a0633a37516609fedaf0f45b5a5'));
            }
        }
    });
    $('#signin_password').blur(function () {
        $('.pw-error').html("");
        var password = $("#signin_password").val().trim();
        if(password){
            if (password.length == 0) {
                $('.pw-error').html($.i18n.prop('05be99945d2379ae060455c9887c4e33'));
            }
        }
    });
    //登录设置
    function loginSubmit() {
        $('.login-error').html('');
        var phone = $("input[name='phone']").val().trim();
        var prefixPhoneNum = $("#prefixPhoneNum").attr('value');
        var phoneresult={}
        console.log(phone)
        if (prefixPhoneNum=='0086') {
            phoneresult = verification.phone(phone)
        } else {
            phoneresult = verification.isNumber(phone)
        }
        var boo = true;
        if(phoneresult.status_code!=0 ||!phone){
            $('.phone-error').html($.i18n.prop('51fd889cc8b548676309c4be8130b2d2'));
            boo = false;
        };
        // 密码登陆
        if(loginType == 'password_login'){
            var password = $("#signin_password").val().trim();
            var remember_me = $('#autologin').data('checked');
            if (password.length == 0) {
                $('.pw-error').html($.i18n.prop('05be99945d2379ae060455c9887c4e33'));
                boo = false;
            }
        }
        // 验证码登陆
        if(loginType =='valid_login'){
            var veriCode = $("input[name='veriCode']").val().trim();
            var remember_me = true;
            var reg = /^\d{4}$/;
            if(!reg.test(veriCode)){
                $('.code-error').html($.i18n.prop('09143a0633a37516609fedaf0f45b5a5'));
                boo = false;
            }
        }
        if(boo){
            var myDate = new Date();
            $.post("/login/c_login", {
                phone: phone,
                prefixPhoneNum: prefixPhoneNum,
                password: password?password:'',
                remember_me: remember_me,
                veriCode:veriCode?veriCode:'',
                vsid:vsid,
                loginType:loginType,
                date: myDate
            }, function (data) {
                $.cookie("autoLogin", 1, {path: '/'}); //让系统不再进行自动登录
                if (data.status_code == 0) {
                    $.cookie("prefixPhoneNum", prefixPhoneNum, {"expires": 10, path: '/'});
                    $.cookie("phone", phone, {"expires": 10, path: '/'});
                    // $.cookie("password", password, {expires: 10, path: '/'});
                    // $.cookie("remember_me", 'true', {expires: 10, path: '/'});


                    var perfectInformationstatus= $.cookie('perfectInformationstatus');
                    //$.cookie("perfectInformationstatus", '0', {expires: 10, path: '/'});//测试用，要改回来
                    ////var perfectInformationstatus= '1';//测试用，要改回来
                    if(perfectInformationstatus=='1')
                    {
                        //追踪路径返回页面
                        var href = location.href;
                        var his_ex = href.indexOf('history');
                        if (his_ex > -1) {
                            his_ex = his_ex + 8;
                            var all_ex = href.length;
                            var history = href.substring(his_ex, all_ex);
                            var host = window.location.host;
                            window.location.replace("//" + host + history);
                        } else {
                            window.location.replace("/");
                        }
                    }
                    else
                    {
                        var history='';
                        var href = location.href;
                        var his_ex = href.indexOf('history');
                        if (his_ex > -1) {
                            his_ex = his_ex + 8;
                            var all_ex = href.length;
                            history = href.substring(his_ex, all_ex);
                        }
                        window.location.replace("/register/p_completion?history="+history);
                    }

                } else {
                    $('#infor_back').html(data.infor_back);
                    $('.alert-info').slideDown();
                    setTimeout(function(){$('.alert-info').slideUp();}, 4000);
                    // var status=data.status_code;
                    /*switch(status){
                        case -1:
                            $('.back_infor_user').html(data.infor_back);
                            break;
                        case -2:
                            $('.back_infor_password').html(data.infor_back);
                            break;
                        default:
                            $('.back_infor_user').html(data.infor_back);
                            break;
                    }*/
                }
            }, "json");
        }
    }
    //点击enter键发送数据
    $("html").off("keydown");
    $("html").on("keydown", function(event) {
        if( $("input[name='phone']").is(":focus")||$("#signin_password").is(":focus")||$("input[name='veriCode']").is(":focus")){
            if (13 === event.keyCode) {
                loginSubmit();
            }
        }

    });
    //登录按钮事件
    $("#user-login-btn").bind("click", function() {
        loginSubmit();
    });

    //进入页面，直接通过ajax查询国家列别
    getCountryList(function(data){
        try{
            if(data.status.status_code==0)
            {
                for(i=0;i<data.result.CountryList.length;i++){
                    $('.prefixPhoneNum_box').append("<li class=prefixNum data-num="+data.result.CountryList[i].phonePrefixNum+">"+data.result.CountryList[i].name+"</li>");
                }
            }
        }
        catch(e){
        }
    });

    $('#autologin').click(function () {
        $(this).data('checked',!$(this).data('checked'));
        var checked = $(this).data('checked');
        if(checked){
            $(this).find('img').attr('src','/images/login/autologincheck.png')
        }else{
            $(this).find('img').attr('src','/images/login/autologinnocheck.png')
        }
    });

    //获取短信验证码
    $("#verification").bind("click",function(){
        $('.login-error').html('');
        var boo = true;
        var prefixPhoneNum = $("#prefixPhoneNum").attr('value');
        var phone = $("input[name='phone']").val().trim();
        var phoneresult=(prefixPhoneNum=='0086')?verification.phone(phone):verification.isNumber(phone);
        if(phoneresult.status_code!=0 ||!phone){
            $('.phone-error').html($.i18n.prop('51fd889cc8b548676309c4be8130b2d2'));
            boo = false;
        };
        if(boo){
            $('#verification').attr('disabled','disabled').addClass('waitbtn');
            $('.login-error').html('');
            var myDate = new Date();
            sendVeriCode_noimg(phone,prefixPhoneNum,2,function(data){
                if(data.status.status_code == 0){
                    vsid = data.result.vsid;
                    $('#verification').html($.i18n.prop('e00d8d9d3a9eb3a5cc87795d32e30aa8'));
                    $('#verification').children('one').html('120');
                    time();
                }else{
                    $('#verification').removeAttr("disabled").removeClass('waitbtn');
                    $('.phone-error').html(data.status.status_reason);
                }
            });
        }
    });

});